package com.lx.algorithm.InsertSort;

import com.lx.algorithm.util.SortUtils;

/**
 * Description:  插入排序
 * Copyright:   Copyright (c)2019
 * Company:     zefu
 *
 * @author: 张李鑫
 * @version: 1.0
 * Create at:   2021-09-01 13:54:02
 * <p>
 * Modification History:
 * Date         Author      Version     Description
 * ------------------------------------------------------------------
 * 2021-09-01     张李鑫                     1.0         1.0 Version
 */
public class InsertionSort {

    /**
     * <p>
     *    插入排序
     *    从左到右保证有序
     * </p>
     * @param  arr
     * @return int[]
     * @Author zlx
     * @date 2021/9/1
     */
    public static  int[] bubbleSort(int []arr){
        for (int i = 0; i < arr.length-1; i++) {
            int j=i+1;
            while (j!=0){
                if (arr[j]<arr[j-1]){
                    SortUtils.swap(j-1,j,arr);
                }
                j--;
            }
        }
        return arr;
    }

    public static  int[] bubbleSort1(int []arr){
        for (int i = 1; i < arr.length; i++) {
            for (int j = i; j >0&&arr[j]<arr[j-1] ; j--) {
                SortUtils.swap(j-1,j,arr);
            }
        }
        return arr;
    }


    public static  int[] bubbleSortByTeach(int []arr){
        for (int i = 1; i < arr.length; i++) {
            for (int j = i-1; j >=0&&arr[j]>arr[j+1] ; j--) {
                SortUtils.swap(j,j+1,arr);
            }
        }
        return arr;
    }

}
